package com.thinkin.services.util;

import java.util.List;
import java.util.UUID;

import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.thinkin.allDo.SysDao;
import com.thinkin.util.JsonHelper;

/**
 * 公共Server接口 概览、明细
 * 
 * @author Administrator
 * 
 */
public class CommonHelper {

	private Logger log = LoggerFactory.getLogger(this.getClass().getName());

	// public static void main(String[] args){
	// CommonHelper com=new CommonHelper();
	// String s=com.DoValiDate("{'cusname':'陈剑清'}", "e_customer");
	// System.out.println(s);
	// }
	/**
	 * 新增
	 * 
	 * @param params
	 * @param clazz
	 * @return
	 */
	public String doSaveAdd(String params, String clazz) {
		String result = "";

		org.json.JSONObject jsonObject = JsonHelper.toJsonString(params);
		String keyid = UUID.randomUUID().toString();
		try {

			jsonObject.put("keyid", keyid);
			params = jsonObject.toString();
			List<String> keys = JsonHelper.toJSONStringKey(params);
			List<Object> values = JsonHelper.toJSONStringValue(params);
			String validate = "_text";
			for (int key = 0; keys != null && keys.size() > 0
					&& key < keys.size(); key++) {
				if (keys.get(key).toString().indexOf(validate) > 0) {
					keys.remove(key);
					values.remove(key);
				}
			}

			BaseService.doSave(keys, values, clazz);
			result = keyid;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error(e.getMessage(), e);
		}
		return "{\"keyid\":\"" + result + "\"}";
	}

	/**
	 * 修改
	 * 
	 * @param params
	 * @param clazz
	 * @return
	 */
	public String doSaveEdit(String params, String clazz) {
		// String result = "";
		String keyid = "";
		org.json.JSONObject jsonObject = JsonHelper.toJsonString(params);
		try {
			keyid = jsonObject.get("keyid").toString();
			List<String> keys = JsonHelper.toJSONStringKey(params);
			List<Object> values = JsonHelper.toJSONStringValue(params);
			String validate = "_text";
			for (int key = 0; keys != null && keys.size() > 0
					&& key < keys.size(); key++) {
				if (keys.get(key).toString().indexOf(validate) > 0) {
					keys.remove(key);
					values.remove(key);
				}
			}
			BaseService.doEdit(keys, values, clazz);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "{\"keyid\":\"" + keyid + "\"}";
	}

	/**
	 * 查询
	 * 
	 * @param params
	 * @param clazz
	 * @return
	 */
	public String doQuery(String params, String clazz) {
		List<String> keys = JsonHelper.toJSONStringKey(params);
		List<Object> values = JsonHelper.toJSONStringValue(params);
		String validate = "_text";
		for (int key = 0; keys != null && keys.size() > 0 && key < keys.size(); key++) {
			if (keys.get(key).toString().indexOf(validate) > 0) {
				keys.remove(key);
				values.remove(key);
			}
		}
		String result = BaseService.doQuery(keys, values.toArray(), clazz);
		return result;
	}

	/**
	 * 删除
	 * 
	 * @param params
	 * @param clazz
	 * @return
	 */
	public String doDelete(String params, String clazz) {

		List<String> keys = JsonHelper.toJSONStringKey(params);
		List<Object> values = JsonHelper.toJSONStringValue(params);
		String result = BaseService.doDelete(keys, values.toArray(), clazz);
		return "{\"result\":\"" + result + "\"}";
	}

	/**
	 * 删除主表的数据 把子表的数据也删除
	 * */
	public String doDeletePFK(String pkeyid, String tablepname, String fparam,
			String tablefname) {
		List<String> fkeys = JsonHelper.toJSONStringKey(fparam);
		List<Object> fvalues = JsonHelper.toJSONStringValue(fparam);
		String result = BaseService.doDeletePFK(pkeyid, tablepname, fkeys,
				fvalues.toArray(), tablefname);
		return "{\"result\":\"" + result + "\"}";

	}

	/**
	 * 加载 单条记录
	 * 
	 * @param params
	 * @param clazz
	 * @return
	 */
	public String doFindEntity(String params, String clazz) {
		List<String> keys = JsonHelper.toJSONStringKey(params);
		List<Object> values = JsonHelper.toJSONStringValue(params);
		String result = BaseService.doFindEntity(keys, values.toArray(), clazz);
		return result;
	}

	/**
	 * 修改时候验证
	 * 
	 * @param params
	 * @param clazz
	 * @return
	 */
	public String doValiDateEdit(String params, String clazz) {
		List<String> keys = JsonHelper.toJSONStringKey(params);
		List<Object> values = JsonHelper.toJSONStringValue(params);
		String result = BaseService.doValidateEdit(keys, values, clazz);
		return result;
	}

	/**
	 * 新增验证
	 * 
	 * @param params
	 * @param clazz
	 * @return
	 */
	public String doValidateAdd(String params, String clazz) {
		List<String> keys = JsonHelper.toJSONStringKey(params);
		List<Object> values = JsonHelper.toJSONStringValue(params);
		String result = BaseService.doValidateAdd(keys, values, clazz);
		return result;
	}

	/**
	 * 加载系统代码 左边树
	 * 
	 * @param params
	 * @param clazz
	 * @return
	 */
	public String baseEcodelibClass(String params, String clazz) {
		String result = BaseService.baseEcodelibClass(clazz);
		return result;
	}

	/**
	 * 初始化 费用类型
	 * 
	 * @param params
	 * @param clazz
	 * @return
	 */
	public String initCosttype(String params, String clazz) {
		String result = BaseService.initCosttype(clazz);
		return result;
	}

	/**
	 * 初始化 城市类型
	 * 
	 * @param params
	 * @param clazz
	 * @return
	 */
	public String initCitytype(String params, String clazz) {
		String result = BaseService.initCitytype(clazz);
		return result;
	}

	/**
	 * 初始化 大区
	 * 
	 * @param params
	 * @param clazz
	 * @return
	 */
	public String initZone(String params, String clazz) {
		String result = BaseService.initZone(clazz);
		return result;
	}

	/**
	 * 初始化小区
	 * 
	 * @param params
	 * @param clazz
	 * @return
	 */
	public String initZoneitem(String params, String clazz) {
		String result = BaseService.initZoneitem(clazz);
		return result;
	}

	/**
	 * 初始化 Eunit
	 */
	public String initEunit(String params, String clazz) {
		org.json.JSONObject jsonObject = JsonHelper.toJsonString(params);
		String param = "";
		try {
			param = jsonObject.get("typelist").toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String result = BaseService.initEunit(param, clazz);
		return result;
	}

	public String initCustomer(String clazz) {
		String result = BaseService.initCustomer(clazz);
		return result;
	}

	/**
	 * 初始化 商品类型
	 * 
	 * @param params
	 * @param clazz
	 * @return
	 */
	public String initSkuType(String params, String clazz) {
		String result = BaseService.initSkuType(clazz);
		return result;
	}

	/**
	 * 初始化 品牌名称
	 * 
	 * @param params
	 * @param clazz
	 * @return
	 */
	public String initBrandName(String params, String clazz) {
		String result = BaseService.initBrandName(clazz);
		return result;
	}

	public String skuQuery(String params, String clazz) {
		org.json.JSONObject jsonObject = JsonHelper.toJsonString(params);
		List<String> keys = null;
		List<Object> values = null;
		String viewTree = "";
		try {
			viewTree = jsonObject.get("viewTree").toString();
			jsonObject.remove("viewTree");
			String param = jsonObject.toString();
			keys = JsonHelper.toJSONStringKey(param);
			values = JsonHelper.toJSONStringValue(param);
			String validate = "_text";
			for (int key = 0; keys != null && keys.size() > 0
					&& key < keys.size(); key++) {
				if (keys.get(key).toString().indexOf(validate) > 0) {
					keys.remove(key);
					values.remove(key);
				}
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String result = BaseService.skuQuery(keys, values.toArray(), viewTree,
				clazz);
		return result;
	}

	/**
	 * 数据验证
	 * 
	 * @param param
	 *            需要验证的字段json格式
	 * @return 返回验证后的json格式
	 */
	public String DoValiDate(String param, String TabeName) {
		String keyid = "";
		String result = "{}";
		try {
			org.json.JSONObject jsonObject = JsonHelper.toJsonString(param);
			if (jsonObject.has("keyid")) {
				keyid = jsonObject.getString("keyid");
				jsonObject.remove("keyid");
			}
			param = jsonObject.toString();
			List<String> keys = JsonHelper.toJSONStringKey(param);
			List<Object> values = JsonHelper.toJSONStringValue(param);
			StringBuffer BuildSQL = new StringBuffer();
			String str_SQL = String.format(
					"select count(1) as data from %1$s where 1=1", TabeName);
			for (int key = 0; keys != null && keys.size() > 0
					&& key < keys.size(); key++) {
				if (BuildSQL.length() > 0) {
					//BuildSQL.append(TiConstants.SqlSplit);
				}
				BuildSQL.append(keys.get(key).toString());
			//	BuildSQL.append(TiConstants.SqlSplit);
				BuildSQL.append(str_SQL);
				BuildSQL.append("  AND " + keys.get(key).toString() + "='"
						+ values.get(key) + "'");
				if (keyid.length() > 0) {
					BuildSQL.append(String.format(" and keyid!='%1$s'", keyid));
				}
			}
			result = SysDao.getCommonServer().doQueryBatch(BuildSQL.toString(),
					true);
		} catch (Exception e) {
			result = "{\"Error\":\'" + e.getMessage() + "\'}";
		}
		return result;
	}

	public String initTree(String params, String clazz) {
		List<String> keys = JsonHelper.toJSONStringKey(params);
		List<Object> values = JsonHelper.toJSONStringValue(params);

		String result = BaseService.initTree(keys, values.toArray(), clazz);
		return result;
	}
}
